<?php
//
// Page Class
// The SDMworld! Network
//

uiLoadLibrary('page');


class clsPage extends cDycmsObject
{
	protected $mClsId = 'Page';

	private $mTitle;
	private $mHtmlBody;
	private $mHtmlHead;
	private $mParent;

	const allowedTags = '<a><p><b><em><i><div><br><img><h1><h2><h3><h4>';
	
	function Render(cDycmsClientNode &$r_client)
	{
		global $gViewerUrl;

		$buf = "<div class=\"ContentTitle\"><a name='page-top'></a> / ";
		$parent = $this->mParent;

		$hier = array();
		do
		{
			$obj = dyLoadObject($this->mClsId, $parent);
			if($obj instanceof cDycmsObject)
			{
				$parent = $obj->Get('obj-parent');
				array_push($hier, $obj);
			};
		}while($obj instanceof cDycmsObject);

		while($obj = array_pop($hier))
		{
			$buf .= "<a href=\"".dyViewerUrl('Page',
				$obj->GetId()) . "\">".$obj->GetSummary() . "</a> / ";
		};

		try
		{
			$q = new cDycmsQuery("obj-parent:".$this->mId);
			$q->SetClass('Page');
			$n = $q->Execute();
		}
		catch(Exception $e)
		{
			dyLog($e->getMessage());
			$n = 0;
		};

		$buf .= $this->mTitle;

		for($c=0; $c<$n; $c++)
		{

		};


		$buf .= "</div>";
		
		//
		// Render data to client
		//

		$r_client->AddChild($buf);
		$r_client->AddChild($body = new cHtmlWrapperTag('div',array(
			'class'=>'ContentBody'
		)));
		
		$body->AddChild(new cUiWikiRenderer(cUiWikiRenderer::$zDefaultToks,
			$this->mHtmlBody, clsPage::allowedTags, array(
				'noParagraph'=>true
		)));
		
		//
		// Page footer
		//
		
		$buf = '<div class="ContentFooter"><a href="#page-top">'.
			uiString("page-top",true) . '</a>';

		if($this->CheckPriv(S_MODIFY))
		{
			$buf .= ' - <a href="'.dyEditorUrl($this->mClsId, $this->mId).'"><span class="icon-edit">Edit this page</span></a>';
		};

		$buf .= '</div>';
		
		$r_client->AddChild($buf);
	}

	function DrawEditor(cUiEditor &$editor)
	{
		//
		// Draw the editor form
		//

		$editor->AddChild(new cUiTextBox(array(
			'name'=>'title',
			'value'=>$this->mTitle
		), 'Page Title'));
		
		$editor->AddChild(new cUiEditorTextArea('htmlbody',
			$this->mHtmlBody, null, false));
		
		$editor->AddDefaultButtons();
	}

	function GetSummary()
	{
		return $this->mTitle;
	}

	function LoadObject()
	{
		$this->mSummaryValue['title'] = true;
		$this->mSummaryValue['obj-parent'] = true;

		$this->mTitle = &$this->mAttributes['title'];
		$this->mHtmlBody = &$this->mAttributes['htmlbody'];
		$this->mHtmlHead = &$this->mAttributes['htmlhead'];
		$this->mParent = &$this->mAttributes['obj-parent'];
	}

	function GetAttributes($bVals)
	{
		if($bVals)
		{

		}
		else
		{
			return array('title', 'htmlbody', 'htmlhead');
		};
	}

    function Create(){}
}

?>
